<?php
namespace BoxSns\Logic;
class GroupLogic extends \LogicController {

	public function _initialize() {
		parent::_initialize();
		$this->group = BoxModel("SnsGroup");
		$this->user_id = $this->user_info['user_id'];
	}

	/*******创建群组*****/
	public function add($row) {
		$error = Logic("BoxSns", "Member")->check($this->user_id);
		if ($error['error'] > 0) {
			return $error;
		}
		if (!$row['group_name']) {
			return array(
				'error' => 2,
				'msg' => '群组名不能为空',
			);
		}
		$row['user_id'] = $this->user_id;
		$r = $this->group->add($row);
		return $r;
	}

	/*****删除群组*****/
	public function del($group_id) {
		$data = $this->group->load($group_id);
		if (!$data) {
			return array(
				'error' => 2,
				'msg' => '群组不存在',
			);
		}
		if ($this->user_id != $data['user_id']) {
			return array(
				'error' => 1,
				'msg' => '你无权操作，群组创始人才能删除',
			);
		}
		$id = $this->group->del_group($group_id);
		return $id;
	}

	/******判断用户是否可以在这里发帖*****/
	public function checkMake($user_id, $group_id) {
		$groupData = BoxModel("SnsGroup")->load($pid);
		$rel = BoxModel("SnsGroup")->getRel($user_id, $group_id);
		if ($groupData['make_topic'] == 1) {
			return array(
				'error' => 0,
				'msg' => '所有人都可以发布',
			);
		}
		if ($groupData['make_topic'] == 2 && $rel['rel'] != "master") {
			return array(
				'error' => 1,
				'msg' => '只有群主可以发言',
			);
		}
		if ($groupData['make_topic'] == 3 && $rel['rel'] != "join") {
			return array(
				'error' => 3,
				'msg' => '只有加入群的才可以发言',
			);
		}
	}

	/***********加入群组****/
	public function join_group($group_id = 0) {
		$error = Logic("BoxSns", "Member")->check($this->user_id);
		if ($error['error'] > 0) {
			return $error;
		}
		$data = BoxModel("SnsGroup")->load($group_id);
		if ($data['is_show'] != 1) {
			return array(
				'error' => 6,
				'msg' => '非法操作',
			);
		}
		if ($data['rel'] == 'master') {
			return array(
				'error' => 1,
				'msg' => '你是群主，还加个毛线',
			);
		}
		if ($data['rel'] == 'join') {
			return array(
				'error' => 2,
				'msg' => '你已加入了该群',
			);
		}
		if ($data['rel'] == 'black') {
			return array(
				'error' => 3,
				'msg' => '你已被拉入黑名单',
			);
		}
		if ($data['rel'] == 'apply') {
			return array(
				'error' => 4,
				'msg' => '你已申请加，等待审核',
			);
		}
		/******需要审核才能加入****/
		if ($data['join_type'] == 1) {
			$model = BoxModel("sns_group_invite");
			$row = array(
				'group_id' => $group_id,
				'user_id' => $data['user_id'],
				'to_user_id' => $this->user_id,
				'dateline' => date("Y-m-d H:i:s"),
				'remark' => '申请加入群',
			);
			$row['hash'] = md5(json_encode($row));
			$r = $model->add($row);
			if ($r) {
				return array(
					'error' => 0,
					'msg' => '申请成功，请等待审核',
				);
			} else {
				return array(
					'error' => 7,
					'msg' => '数据保存失败',
				);
			}
		} else {
			$r = BoxModel("SnsGroup")->join_group($this->user_id, $group_id);
			if ($r) {
				return array(
					'error' => 0,
					'msg' => '操作成功',
				);
			} else {
				return array(
					'error' => 7,
					'msg' => '数据保存失败',
				);
			}
		}
	}

	/*********退出群组***/
	public function quit_group($group_id) {
		$r = BoxModel("SnsGroup")->quit_group($this->user_id, $group_id);
		if ($r) {
			return array(
				'error' => 0,
				'msg' => '操作成功',
			);
		} else {
			return array(
				'error' => 7,
				'msg' => '数据保存失败',
			);
		}
	}

	/*********同意加入****/
	public function join_agree($hash) {
		$model = BoxModel("sns_group_invite");
		$data = $model->where("hash='$hash'")->find();
		if ($data) {
			$r = $this->group->join_group($data['to_user_id'], $data['group_id']);
			$model->where("hash='$hash'")->delete();
			return array(
				'error' => 0,
				'msg' => '操作成功',
			);
		} else {
			return array(
				'error' => 7,
				'msg' => '非法操作',
			);
		}
	}

}